<template>
  <div class="settings-container">
    <div class="app-container">
      <el-card>
        <!-- 新增角色按钮 -->
        <el-row style="height: 60px">
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            @click="showDialogFn"
          >新增角色</el-button>
        </el-row>
        <!-- 新增弹框 -->
        <el-dialog :title="roleForm.id?'编辑角色':'新增角色'" :visible="showDialog" @close="closeDialog">
          <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
            <el-form-item label="角色名称" prop="name">
              <el-input v-model="roleForm.name" />
            </el-form-item>
            <el-form-item label="角色描述">
              <el-input v-model="roleForm.description" />
            </el-form-item>
          </el-form>
          <!-- 底部 -->
          <el-row slot="footer" type="flex" justify="center">
            <el-col :span="6">
              <el-button size="small" @click="closeDialog">取消</el-button>
              <el-button size="small" type="primary" @click="btnOK">确定</el-button>
            </el-col>
          </el-row>
        </el-dialog>

        <!-- 表格 -->
        <el-table :data="roleList">
          <el-table-column label="序号" width="120" type="index" />
          <el-table-column label="角色名称" width="240" prop="name" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="scope">
              <el-button size="small" type="success">分配权限</el-button>
              <el-button size="small" type="primary" @click="editRoleFn(scope.row.id)">编辑</el-button>
              <el-button size="small" type="danger" @click.native="deleteRole(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <!-- 分页组件 -->
          <el-pagination
            layout="prev,pager,next"
            :total="total"
            :page-size="params.pagesize"
            @current-change="changePage"
          />
        </el-row>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getRoleList, deleteRole, addRole, getRoleDetail, updateRole } from '@/api/settings'
export default {
  data() {
    return {
      roleList: [],
      params: {
        // 放置页码及相关数据
        page: 1,
        pagesize: 2
      },
      total: 0,
      roleForm: {
        name: '',
        description: ''
      },
      showDialog: false, // 控制弹出层显示隐藏
      // 弹出层校验规则
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      }
    }
  },
  mounted() {
    // 获取角色列表
    this.hGetRoleList()
  },
  methods: {
    // 获取角色列表
    async  hGetRoleList() {
      const res = await getRoleList(this.params)
      console.log(res)
      // 给角色列表赋值
      this.roleList = res.data.rows
      // 给分页的total赋值
      this.total = res.data.total
    },
    // 分页
    changePage(page) {
      this.params.page = page
      // 用当前的页面去请求数据
      this.hGetRoleList()
    },
    // 删除角色
    deleteRole(id) {
      this.$confirm('删除角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 调用删除接口
        await deleteRole(id)
        // 更新列表
        this.hGetRoleList()
        // 提示用户
        this.$message.success('删除角色成功')
      }).catch()
    },
    // 显示弹出层
    showDialogFn() {
      this.showDialog = true
    },
    // 添加角色 兜底校验
    btnOK() {
      this.$refs.roleForm.validate(async(valid) => {
        if (valid) {
          if (this.roleForm.id) {
            // 编辑
            await updateRole(this.roleForm)
          } else {
            // 调用添加接口
            await addRole(this.roleForm)
          }
          // 重新拉取数据
          this.hGetRoleList()
          this.$message.success('操作成功')
          this.showDialog = false
        }
      })
    },
    // 关闭弹出层
    closeDialog() {
      this.roleForm = {
        name: '',
        description: ''
      }
      // 移除校验
      this.$refs.roleForm.resetFields()
      // 关闭弹框
      this.showDialog = false
    },
    // 编辑
    editRoleFn(id) {
      // 1.展开弹框
      this.showDialog = true
      // 回填数据
      this.hGetRoleDetail(id)
      // 更新列表
    },
    // 获取角色详情。回填数据
    async hGetRoleDetail(id) {
      const res = await getRoleDetail(id)
      // 回填操作
      console.log(res)
      this.roleForm = res.data
    }
  }
}
</script>

<style lang="less" scoped>
</style>
